package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class FacebookAnalyse implements AnalyseNews {

	private static final String[] regex={
		"https://.*.facebook.com/.*/posts/[0-9]*",
		"https://.*.facebook.com/.*/photos/.*[0-9]{10,}/[0-9]{10,}/.*",
		"https://.*.facebook.com/.*/videos/[0-9]{10,}/",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if(url.contains("/posts/")){
			String html = doc.select(".hidden_elem").html();
			if(html!=null) html = html.replace("<!--", "");
			if(html!=null) html = html.replace("-->", "");
			doc = Jsoup.parse(html);
		}
//		System.out.println(html);
		title =doc.select(".userContent").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("#fbPhotoPageCaption").text();
		}
		if(title!=null){
			if(title.length()>80){
				title = StringUtils.left(title,80) + "...";
			}
		}
		if(StringUtils.isBlank(title)){
			news.setFixTitle(false);
		}
		date= DateUtils.matchDate(doc.select(".timestampContent").text()+"前");
		if(date==null){
			date= DateUtils.matchDate(doc.select(".livetimestamp").text()+"前");
		}
		author= doc.select(".fwb").text();
		if(StringUtils.isBlank(author)){
			author= doc.select("#fbPhotoPageAuthorName").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("#photoborder,#fbPhotoPageCaption"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".userContent"));
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
